#include "Reflector.h"


Reflector::Reflector() : tex_reflections(0), rtv_reflections(0), srv_reflections(0), tex_reflections_depth(0), dsv_reflections_depth(0)
{
	tex_reflections_depth = 0;
	dsv_reflections_depth = 0;
}


Reflector::~Reflector()
{
	try
	{
		if (srv_reflections)
			srv_reflections->Release();
		
		if (tex_reflections)
			tex_reflections->Release();
		
		if (rtv_reflections)
			rtv_reflections->Release();
		
		if (tex_reflections_depth)
			tex_reflections_depth->Release();
		
		if (dsv_reflections_depth)
			dsv_reflections_depth->Release();

	}
	catch(...)
	{
		MessageBoxA(0, "Exception thrown in Reflector destructor", 0, 0);
	}
}



HRESULT Reflector::createReflectionTexture2D(ID3D11Device* g_dev, const D3D11_TEXTURE2D_DESC& desc)
{
	return g_dev->CreateTexture2D(&desc, 0, &tex_reflections);
}

HRESULT Reflector::createDepthTexture2D(ID3D11Device* g_dev, const D3D11_TEXTURE2D_DESC& desc)
{
	return g_dev->CreateTexture2D(&desc, 0, &tex_reflections_depth);
}

HRESULT Reflector::createRenderTargetView(ID3D11Device* g_dev, const D3D11_RENDER_TARGET_VIEW_DESC& rtv_desc)
{
	return g_dev->CreateRenderTargetView(tex_reflections, &rtv_desc, &rtv_reflections);
}

HRESULT Reflector::createShaderResourceView(ID3D11Device* g_dev, const D3D11_SHADER_RESOURCE_VIEW_DESC& srv_desc)
{
	return g_dev->CreateShaderResourceView(tex_reflections, &srv_desc, &srv_reflections);
}

HRESULT Reflector::createDepthStencilView(ID3D11Device* g_dev, const D3D11_DEPTH_STENCIL_VIEW_DESC& dsv_desc)
{
	return g_dev->CreateDepthStencilView(tex_reflections_depth, &dsv_desc, &dsv_reflections_depth);
}

void Reflector::setRenderTarget(ID3D11DeviceContext* g_context) const
{
	ID3D11RenderTargetView* rtv[1] = { rtv_reflections };

	float black[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
	g_context->ClearRenderTargetView(rtv_reflections, black);
	g_context->ClearDepthStencilView(dsv_reflections_depth, D3D11_CLEAR_DEPTH, 1.0f, 0xff);
	g_context->OMSetRenderTargets(1, rtv, dsv_reflections_depth); 

}

void Reflector::passReflectionSRV(ID3DX11EffectShaderResourceVariable** fx_srv)
{
	(*fx_srv)->SetResource(this->srv_reflections);
}